//
// Created by HZH on 2023-04-29.
//
#include <stdio.h>
#include "BzDB_binary_operate.h"


/**
 * 把二进制数组转换为数字
 * 有且只能有八个字符
 * [0,0,0,0,0,0,1,1] -> 3
 *
 *     int bin_list[] = {1,1,1,1,1,1,1,0};
 *   unsigned char res = BzDB_binary_convert_bin_to_uchar(bin_list,8);
 *   printf("res: %d\n ",res);
 * @param binary_list
 * @return
 */
unsigned char BzDB_binary_convert_bin_to_uchar(int binary_list[],int len){
    if(len!=8){
        printf("the len of binary_list must be 8\n");
        return 0;
    }

    unsigned char res = 0;
    for(int i = 0 ; i < 8 ; i++){
        res = res*2 + binary_list[i];
    }
    return res;
}


/**
    int binary_list[16] = {1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1};
    unsigned char c_list[2];
    BzDB_binary_convert_bin_list_to_uchar(binary_list,16 , c_list , 2);
    for(int i = 0 ; i< 2 ; i++){
        printf("%d ",c_list[i]);
    }
 * @param binary_list
 * @return
 */
void BzDB_binary_convert_bin_list_to_uchar(int binary_list[],int b_len , unsigned char c_list[] , int c_len){
    if(c_len * 8 != b_len){
        printf("c_len * 8 == b_len");
        return;
    }

    int b_index = 0;

    for(int i = 0 ; i< c_len ; i++){
        int binary_temp[8] = {0};
        for(int j = 0 ; j< 8 ; j++){
            binary_temp[j] = binary_list[b_index];
            b_index = b_index + 1;
        }
        c_list[i] = BzDB_binary_convert_bin_to_uchar(binary_temp,8);
    }
}


/**
 * 把uchar转换为二进制数组
 *     int binary_list[8];
 *   BzDB_binary_convert_uchar_to_bin(254, binary_list,8);
 *   for(int i = 0 ; i < 8 ; i++){
 *       printf("%d\t",binary_list[i]);
 *   }
 * @param c
 * @param binary_list
 * @param len
 */
void BzDB_binary_convert_uchar_to_bin(unsigned char c, int binary_list[],int len){

    if(len!=8){
        printf("the len of binary_list must be 8\n");
        return;
    }
    for(int i = 0 ; i < 8 ; i++){
        binary_list[i] = 0;
    }
    /**
     *   4 % 2 == 0    4/2 = 2;
     *   2 % 2 == 0    2/2 = 1
     *   1 % 2 == 1    1/2 = 0;
     */

    int binary_index = 7;

    while (c != 0){
        if(c % 2 == 0){
            binary_list[binary_index] = 0;
        }else{
            binary_list[binary_index] = 1;
        }
        binary_index = binary_index - 1;
        c = c/2;
    }

}


/**
     unsigned char c_list[] = { 1, 2,4};
    int binary_list[24] = {0};
    BzDB_binary_convert_uchar_list_to_bin(c_list, 3 , binary_list,24);

    for(int i = 0 ; i < 24 ; i++){
        printf("%d,",binary_list[i]);
        if((i+1)%8==0){
            printf("|");
        }
    }
    @desc 把uchar转换为
 * @param c_list
 * @param c_len
 * @param binary_list
 * @param b_len
 */
void BzDB_binary_convert_uchar_list_to_bin(unsigned char c_list[],int c_len , int binary_list[],int b_len){
    if( 8 * c_len != b_len  ||   b_len % 8 != 0){
        printf("8 * c_len == b_len &&  b_len % 8 == 0  \n");
        return;
    }

    int binary_index = 0;
    for(int i = 0 ; i < c_len ; i++){
        int binary_temp[8] = {0};
        BzDB_binary_convert_uchar_to_bin(c_list[i], binary_temp,8);
        for(int j = 0 ; j < 8 ; j++){
            binary_list[binary_index] = binary_temp[j];
            binary_index = binary_index+1;
        }
    }
}
